/*
*    Phantom Test Driver v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/*
 * SuitePanelBase.java
 *
 * Created on December 5, 2005, 10:01 PM
 */

package ptd_suite_objects;

import javax.swing.text.*;
import java.io.File;
import ptd.PTDMain;

import javax.swing.JTextField;
import javax.swing.JFileChooser;
import ptd.PTDFileFilter;

/**
 *
 * @author  John
 */
public class TestcasePanel extends SuitePanelBase {
    
    private SuiteTestcase myTestcase;
    
    /** Creates new form SuitePanelBase */
    public TestcasePanel() {
        initComponents();
        FileLabel.setText("File: ");
        ExecLabel.setText("Executable: ");
        ArgumentsLabel.setText("Arguments: ");
        //OutputLabel.setText("Output: ");
        //BaseLabel.setText("Base: ");
        UsePhantomCheck.setText("Use Phantom");
        
        //CompareCheck.setText("Compare Output");
        FileBrowse.setText("...");
        ExecBrowse.setText("...");
        //OutputBrowse.setText("...");
        //BaseBrowse.setText("...");

        //BaseText.setEnabled(false);
        //OutputText.setEnabled(false);
        ExecText.setEnabled(false);
        UsePhantomCheck.setSelected(true);

        myTestcase = null;
    }

    public String openFile(){
        return PTDMain.openPSCFile();
        
        /*
        JFileChooser aChooser = new JFileChooser();
        PTDFileFilter fun_filter = new PTDFileFilter("fun", "Phantom Functions (.fun)");
        PTDFileFilter psc_filter = new PTDFileFilter("psc", "Phantom Scripts (.psc)");
        aChooser.setCurrentDirectory(PTDMain.g_CurrentDir);
        aChooser.addChoosableFileFilter(fun_filter);
        aChooser.addChoosableFileFilter(psc_filter);
        
        int result = aChooser.showOpenDialog(this);
        if(result == JFileChooser.CANCEL_OPTION){
            return Ret;
        }
        
        try {            
            File afile = aChooser.getSelectedFile();
            PTDMain.g_CurrentDir = afile.getParentFile();
            return afile.getAbsolutePath();
            //addScriptPane(afile.getAbsolutePath());
        }catch(Exception e){
            Main.debug_out(e.getMessage());
        }
        return Ret;
         */
    }
    
    public void clearFields(){
        //BaseText.setText("");
        //OutputText.setText("");
        ExecText.setText("");
        DescriptionText.setText("");
        FileText.setText("");
        NameText.setText("");
        ArgumentsText.setText("");
        
        UsePhantomCheck.setSelected(true);
        //CompareCheck.setSelected(false);
    }
    
    public SuiteBase getFields(){
        if(myTestcase == null){
            return null;
        }
        SuiteTestcase oldTestcase = new SuiteTestcase(myTestcase);
        
        myTestcase.myName = NameText.getText();
        myTestcase.myFile = FileText.getText();
        myTestcase.myExec = ExecText.getText();
        myTestcase.myArguments = ArgumentsText.getText();
        myTestcase.myDescription = DescriptionText.getText();
        //myTestcase.myBase = BaseText.getText();
        //myTestcase.myOutput = OutputText.getText();
        
        myTestcase.myPostprocessor = PostProcessorText.getText();
        myTestcase.myPreprocessor = PreProcessorText.getText();

        
        //if(CompareCheck.isSelected()){
        //    myTestcase.CompareOutput = 1;
        //}else{
        //    myTestcase.CompareOutput = 0;
        //}
        
        if(UsePhantomCheck.isSelected()){
            myTestcase.UsePhantom = 1;
        }else{
            myTestcase.UsePhantom = 0;
        }
        
        if(myTestcase.isEqualTo(oldTestcase)){
        //    setModified(false);
        }else{
            myTestcase.setModified();
        //    setModified(true);
        //    myTestcase = newTestcase;
        }
        
        return (SuiteBase)myTestcase;
    }
    
    public SuiteBase getNewData(){
        // -- SUITE MODIFIED --
        //setModified(true);
        myTestcase = new SuiteTestcase();
        // Check for correct name
        return getFields();
    }
    
    public SuiteBase getData(){
        return (SuiteBase)myTestcase;
    }
    
    
    public void setFields(SuiteBase InBase){
        // -- SUITE MODIFIED --
        //setModified(false);
        myTestcase = (SuiteTestcase)InBase;
        
        NameText.setText(myTestcase.myName);
        FileText.setText(myTestcase.myFile);
        
        ExecText.setText(myTestcase.myExec);
        ArgumentsText.setText(myTestcase.myArguments);
        DescriptionText.setText(myTestcase.myDescription);
        //BaseText.setText(myTestcase.myBase);
        //OutputText.setText(myTestcase.myOutput);
        
        PreProcessorText.setText(myTestcase.myPreprocessor);
        PostProcessorText.setText(myTestcase.myPostprocessor);
        
        //if(myTestcase.CompareOutput > 0){
        //    CompareCheck.setSelected(true);
        //    BaseText.setEnabled(true);
        //    OutputText.setEnabled(true);
        //    OutputBrowse.setEnabled(true);
        //    BaseBrowse.setEnabled(true);
        //}else{
        //    CompareCheck.setSelected(false);
        //    BaseText.setEnabled(false);
        //    OutputText.setEnabled(false);
        //    OutputBrowse.setEnabled(false);
        //    BaseBrowse.setEnabled(false);            
        //}
        
        if(myTestcase.UsePhantom > 0){
            UsePhantomCheck.setSelected(true);
            ExecText.setEnabled(false);
            ExecBrowse.setEnabled(false);
        }else{
            UsePhantomCheck.setSelected(false);
            ExecText.setEnabled(true);
            ExecBrowse.setEnabled(true);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        FileLabel = new javax.swing.JLabel();
        FileText = new javax.swing.JTextField();
        ExecLabel = new javax.swing.JLabel();
        ExecText = new javax.swing.JTextField();
        UsePhantomCheck = new javax.swing.JCheckBox();
        FileBrowse = new javax.swing.JButton();
        ExecBrowse = new javax.swing.JButton();
        ArgumentsLabel = new javax.swing.JLabel();
        ArgumentsText = new javax.swing.JTextField();

        FileLabel.setOpaque(true);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        add(FileLabel, gridBagConstraints);

        FileText.setColumns(10);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 2;
        gridBagConstraints.ipady = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        add(FileText, gridBagConstraints);

        ExecLabel.setOpaque(true);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(ExecLabel, gridBagConstraints);

        ExecText.setColumns(10);
        ExecText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExecTextActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 2;
        gridBagConstraints.ipady = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        add(ExecText, gridBagConstraints);

        UsePhantomCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UsePhantomCheckActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(UsePhantomCheck, gridBagConstraints);

        FileBrowse.setToolTipText("Browse Files");
        FileBrowse.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        FileBrowse.setPreferredSize(new java.awt.Dimension(20, 8));
        FileBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FileBrowseActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(FileBrowse, gridBagConstraints);

        ExecBrowse.setToolTipText("Browse Executables");
        ExecBrowse.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        ExecBrowse.setPreferredSize(new java.awt.Dimension(20, 8));
        ExecBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExecBrowseActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(ExecBrowse, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(ArgumentsLabel, gridBagConstraints);

        ArgumentsText.setColumns(10);
        ArgumentsText.setMinimumSize(new java.awt.Dimension(6, 22));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        add(ArgumentsText, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void ExecBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExecBrowseActionPerformed
        String sFile = openFile();
        if(sFile != ""){
            ExecText.setText(sFile);
        }
    }//GEN-LAST:event_ExecBrowseActionPerformed

    private void FileBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FileBrowseActionPerformed
        String sFile = openFile();
        if(sFile != ""){
            FileText.setText(sFile);
        }
    }//GEN-LAST:event_FileBrowseActionPerformed

    private void UsePhantomCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UsePhantomCheckActionPerformed
        if(UsePhantomCheck.isSelected()){
            ExecText.setEnabled(false);
            ExecBrowse.setEnabled(false);
        }else{
            ExecText.setEnabled(true);
            ExecBrowse.setEnabled(true);
        }
    }//GEN-LAST:event_UsePhantomCheckActionPerformed

    private void ExecTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExecTextActionPerformed
// TODO add your handling code here:
    }//GEN-LAST:event_ExecTextActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel ArgumentsLabel;
    private javax.swing.JTextField ArgumentsText;
    private javax.swing.JButton ExecBrowse;
    private javax.swing.JLabel ExecLabel;
    private javax.swing.JTextField ExecText;
    private javax.swing.JButton FileBrowse;
    private javax.swing.JLabel FileLabel;
    private javax.swing.JTextField FileText;
    private javax.swing.JCheckBox UsePhantomCheck;
    // End of variables declaration//GEN-END:variables
    
}
